package com.hejjon.service.impl;

import com.hejjon.core.domain.entity.FeaturePublishRecord;
import com.hejjon.core.domain.vo.RecordRelTableInfoVO;
import com.hejjon.core.enumer.PublishStatusEnum;
import com.hejjon.core.utils.StrUtil;
import com.hejjon.domain.bo.RunSqlBO;
import com.hejjon.domain.dto.RecordRelTableInfoDTO;
import com.hejjon.domain.request.BusiFeaturePublishRequest;
import com.hejjon.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BusiFeaturePublishServiceImpl implements IBusiFeaturePublishService {

    @Resource
    private IFeaturePublishRecordService featurePublishRecordService;

    @Resource
    private IBusinessFeatureService businessFeatureService;

    @Resource
    private ISysDatabaseService sysDatabaseService;

    @Resource
    private IRecordRelTableInfoService recordRelTableInfoService;


    @Override
    public boolean publish(BusiFeaturePublishRequest request) {
        PublishStatusEnum statusEnum = PublishStatusEnum.getInstance(request.getPublishStatus());
        String recordId = request.getRecordId();
        boolean res = false;
        switch (statusEnum) {
            case NEW_PUBLISH:
                res = newPublish(recordId);
                break;
            case UPDATE_PUBLISH:
                res = updatePublish(recordId);
                break;
            default:
                return false;
        }
        return res;
    }



    private boolean newPublish(String recordId) {
        FeaturePublishRecord record = featurePublishRecordService.getById(recordId);
        String databaseId = record.getDatabaseId();
        RunSqlBO runSqlBO = sysDatabaseService.loadRunSqlBO(databaseId);
        List<RecordRelTableInfoVO> recordRelFeatureVOS = recordRelTableInfoService.listByRecordId(recordId);

        List<RecordRelTableInfoDTO> tableInfoDTOList = new ArrayList<>();
        for (RecordRelTableInfoVO relVo : recordRelFeatureVOS) {
            String featureId = relVo.getFeatureId();
            String tableName = buildTableName(relVo.getFeatureName());
            String featureLabel = relVo.getFeatureLabel();
            String createTableSql = businessFeatureService.getCreateTableSql(featureId, tableName, featureLabel);
            sysDatabaseService.executeCreateTable(runSqlBO.getConnection(), runSqlBO.getDatabaseName(), createTableSql);

            RecordRelTableInfoDTO dto = new RecordRelTableInfoDTO();
            dto.setDatabaseId(databaseId);
            dto.setDatabaseName(runSqlBO.getDatabaseName());
            dto.setRecordId(recordId);
            dto.setFeatureId(featureId);
            dto.setTableName(tableName);
            tableInfoDTOList.add(dto);
        }

        boolean res = recordRelTableInfoService.batchUpdate(tableInfoDTOList);
        featurePublishRecordService.updatePublishStatus(recordId, PublishStatusEnum.PUBLISHEED.getCode());
        if (res) {
            log.info("保存发布表信息成功!");
            return true;
        }
        return false;
    }

    private boolean updatePublish(String recordId) {
        return false;
    }

    private String buildTableName(String featureName) {
        return StrUtil.camelToSnakeCase(featureName) + "_" + StrUtil.randomInt();
    }






}
